/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tuoc;

import java.sql.ResultSet;
import java.util.Map;
import java.util.Vector;

/**
 *
 * @author apple
 */
public class user_model extends My_model{
    int id;
    String username;
    String password;
    String group;
    
    public Vector get_elements()
    {
        ResultSet rs = this.executeSqlString("SELECT * FROM user_model");
        return this.to_domain_models(rs);
    }
    
    public user_model get_element_by_id(String id)
    {
        ResultSet rs = this.executeSqlString("SELECT * FROM user_model WHERE id = " + id);
        return this.to_domain_model(rs);
    }
    
    public void insert_data(Map<String,String> data)
    {
        String sqlString = String.format("INSERT INTO user VALUES(NULL,'%s','%s','%s')",
                (data.get("username") != null) ? data.get("username") : "",
                (data.get("password") != null) ? data.get("password") : "",
                (data.get("group") != null) ? data.get("group") : ""
                );
        this.insertDatabase(sqlString);
    }
    
    public user_model to_domain_model(ResultSet rs)
    {
        user_model result = new user_model();
        try {
            if(rs.next())
            {
                result.id = rs.getInt("id");
                result.username = rs.getString("user_name");
                result.password = rs.getString("password");
                result.group = rs.getString("group");
            }    
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
    
    public Vector to_domain_models(ResultSet rs)
    {
        Vector result = new Vector();
        try {
            while(rs.next())
            {
                user_model item = new user_model();
                item.username = rs.getString("user_name");
                item.password = rs.getString("password");
                item.group = rs.getString("group");
                
                result.add(item);
            }
            
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
}
